<?php

/**
 * This is the model class for table "xman_skill_categories".
 *
 * The followings are the available columns in table 'xman_skill_categories':
 * @property integer $skill_categories_id
 * @property string $skill_categories_name
 * @property integer $skill_categories_parent_id
 *
 * The followings are the available model relations:
 * @property Skill[] $skills
 * @property SkillCategories $skillCategoriesParent
 * @property SkillCategories[] $skillCategories
 */
class SkillCategories extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return SkillCategories the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_skill_categories';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('skill_categories_id, skill_categories_name', 'required'),
			array('skill_categories_id, skill_categories_parent_id', 'numerical', 'integerOnly'=>true),
			array('skill_categories_name', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('skill_categories_id, skill_categories_name, skill_categories_parent_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'skills' => array(self::HAS_MANY, 'Skill', 'skill_categories_id'),
			'skillCategoriesParent' => array(self::BELONGS_TO, 'SkillCategories', 'skill_categories_parent_id'),
			'skillCategories' => array(self::HAS_MANY, 'SkillCategories', 'skill_categories_parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'skill_categories_id' => 'Skill Categories',
			'skill_categories_name' => 'Skill Categories Name',
			'skill_categories_parent_id' => 'Skill Categories Parent',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('skill_categories_id',$this->skill_categories_id);
		$criteria->compare('skill_categories_name',$this->skill_categories_name,true);
		$criteria->compare('skill_categories_parent_id',$this->skill_categories_parent_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * @return array list category which have parent is program
	 */
	public static function listProgram() {
		
		$programCate = SkillCategories::model()->findByAttributes(array('skill_categories_name'=>'program'));
		if (!$programCate)
			Yii::app()->end('DB error');
		$skillCates = $programCate->skillCategories;
		$list = array();
		foreach ($skillCates as $skillCate) {
			$list[$skillCate->skill_categories_id] =  $skillCate->skill_categories_name;
		}
		return $list;
	}
}